package com.judicial.app.utils;

import java.io.File;
import java.io.FileOutputStream;

import android.content.Context;

public class FileUtil {
	private static File cacheDir;
	private static File image;

	public static void initFileCache(Context context) {
		// Find the dir to save cached images
		if (android.os.Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED))
			cacheDir = new File(
					android.os.Environment.getExternalStorageDirectory(),
					"logistics");
		else
			cacheDir = context.getCacheDir();

		if (!cacheDir.exists())
			cacheDir.mkdirs();
		image = new File(cacheDir, "image");

		if (!image.exists())
			image.mkdirs();

	}

	public static File getFile(String filename) {
		File f = new File(image, filename);
		return f;

	}

	public static File getPhotoFile(String path) {
		File f = new File(image, path);
		return f;
	}

	// private File getCacheDir() {
	// return cacheDir;
	// }
	//
	// private void setCacheDir(File cacheDir) {
	// this.cacheDir = cacheDir;
	// }

	@SuppressWarnings("unused")
	private void clear() {
		File[] files = cacheDir.listFiles();
		for (File f : files)
			f.delete();
	}

	public static void saveLog(String name, String filecontent) {
		try {
			File file = getPhotoFile(name + ".txt");
			FileOutputStream outStream = new FileOutputStream(file);
			outStream.write(filecontent.getBytes());
			outStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
